package com.xing.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xing.dto.LiveAttentionUserDTO;
import com.xing.entity.LiveAttentionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LiveAttentionDao extends BaseMapper<LiveAttentionEntity> {


    @Select("select id, nickname, head_url\n" +
            "from sys_user\n" +
            "where id in (select attentioned_id from live_attention where attention_id = #{userId})\n" +
            "  and sys_user.del_flag = 0;")
    List<LiveAttentionUserDTO> selectAttentionListByUserId(@Param("userId") Long userId);

    @Select("select id, nickname, head_url\n" +
            "from sys_user\n" +
            "where id in (select attention_id from live_attention where attentioned_id = #{userId})\n" +
            "  and sys_user.del_flag = 0;")
    List<LiveAttentionUserDTO> selectAttentionedListByUserId(@Param("userId") Long id);


    @Select("select u.id, nickname, " +
            "       s.url                                 as head_url,\n" +
            " note, " +
            "count(distinct la.id) attention, COUNT(distinct l.id) attentioned,\n" +
            "       IF(STRCMP('live',ro.name)=0, 1, 0) as live\n" +
            "from sys_user u\n" +
            "         left join live_attention la on u.id = la.attention_id\n" +
            "         left join live_attention l ON u.id = l.attentioned_id\n" +
            "         left join sys_user_role r ON r.user_id = u.id\n" +
            "         LEFT JOIN sys_role ro ON r.role_id = ro.id\n" +
            "         LEFT JOIN sys_oss s ON s.id = u.head_url\n" +
            "where u.id in (SELECT attentioned_id FROM live_attention WHERE attention_id = #{userId})\n" +
            "  and u.del_flag = 0\n" +
            "group by u.id,r.role_id\n"+
            "LIMIT ${page},${limit};")
    List<LiveAttentionUserDTO> selectAttentionPageByUserId(@Param("userId") Long userId,
                                                           @Param("page") Integer page,
                                                           @Param("limit") Integer imit);




    @Select("select u.id,\n" +
            "       nickname,\n" +
            "       s.url                                 as head_url,\n" +
            "       note,\n" +
            "       count(distinct la.id)                    attention,\n" +
            "       COUNT(distinct l.id)                     attentioned,\n" +
            "       IF(STRCMP('live', ro.name) = 0, 1, 0) as live\n" +
            "from sys_user u\n" +
            "         left join live_attention la on u.id = la.attention_id\n" +
            "         left join live_attention l ON u.id = l.attentioned_id\n" +
            "         left join sys_user_role r ON r.user_id = u.id\n" +
            "         LEFT JOIN sys_role ro ON r.role_id = ro.id\n" +
            "         LEFT JOIN sys_oss s ON s.id = u.head_url\n" +
            "where u.id in (SELECT attention_id FROM live_attention WHERE attentioned_id = #{userId})\n" +
            "  and u.del_flag = 0\n" +
            "group by u.id, r.role_id \n" +
            "LIMIT ${page},${limit};")
    List<LiveAttentionUserDTO> selectAttentionedPageByUserId(@Param("userId") Long id,
                                                             @Param("page") Integer page,
                                                             @Param("limit") Integer limit);


    @Select("SELECT COUNT(*) FROM live_attention WHERE attention_id = #{attentionId} AND attentioned_id = #{attentionedId} AND del_flag = '0'")
    public Integer getIsAndAttention(@Param("attentionId") String attentionId, @Param("attentionedId") String attentionedId);
}


